#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;

int n, a[N], stk[N], top;
ll ans;

void solve() {
	n = in; rep(i, 1, n) a[i] = in; ans = 0;
	rep(l, 1, n) {
		top = 0; int mx = n + 1000;
		rep(r, l, n) {
			if(a[r] < l) { if(mx > r) ans++; continue; }
			while(top && a[r] <= a[stk[top]]) mx = min(mx, a[stk[top]]), top--;
			stk[++top] = r; if(mx <= r) continue; ans++;
		}
	} printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    int t = in;
	while(t--) solve();
}
